﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace CashNote
{
    public partial class Login : Form
    {
        private connTest conntest = new connTest();
        private MainF mainf = new MainF();
        private Global g = new Global();
        public Login()
        {
            InitializeComponent();
        }

        private void b_exit_Click(object sender, EventArgs e)
        {
                this.Close();
        }

        private void b_login_Click(object sender, EventArgs e)
        {
            string s;
            string pwd;
            string user;
            user = this.tb_user.Text.Trim();
            
            Regex reg = new Regex("'");
            Match mat = reg.Match(user);
            if (mat.Success)
            {
                MessageBox.Show("用户名非法");//位置
                return;
            } 

            if (Global.StrUser.Equals(user))
            {
                MessageBox.Show("该用户已登陆");
                return;
            }
            pwd = this.tb_pwd.Text.Trim();

            if (pwd.Length>16)
            {
                MessageBox.Show("密码长度必须在16位以内");
            }

            EncryptString es = new EncryptString();
            pwd = es.DesEncrypt(pwd);

            s = "select staff_name from sys_staff where staff_id='" + user + "' and password='" + pwd + "'";

            try
            {
                Global.StrUserName = conntest.SelectOnestr(s,Global.Conn);
                if (Global.StrUserName == null)
                {
                    MessageBox.Show("用户名或者密码错");
                }
                else
                {
                    //修改标题栏
                    //SetFramTitle(Global.StrUserName);
                    Global.StrUser = user;
                    mainf.Text = "管理员";
                    this.Close(); 
                }
            }
            catch (NullReferenceException ex)
            {
                //Console.WriteLine("{0} Caught exception #1.", ex);
                MessageBox.Show(ex.ToString(), "登陆失败");
                g.setGlobalNull();

            }
            //finally 
            //{
            //    //Global.conn = null;
            //}
        }

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!Global.StrUserName.Equals(""))
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            
        }

        private void tb_user_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}"); 
            }
        }

        private void tb_pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

    }
}
